/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.Player;

import Various.Tools;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JColorChooser;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class JPopupMenuColor extends JPopupMenu {

    private JMenuItem jMenuItemModifColor;
    private JMenuItem jMenuItemSupprColor;
    private JDialogColorSettings JDParent;

    public JPopupMenuColor(JDialogColorSettings JDParent) {
        super();
        this.JDParent = JDParent;
        jMenuItemModifColor = new JMenuItem("Modifier");
        jMenuItemSupprColor = new JMenuItem("Supprimer");
        jMenuItemSupprColor.setEnabled(false);

        jMenuItemModifColor.addActionListener(new ActionPopupMenuColor());
        jMenuItemSupprColor.addActionListener(new ActionPopupMenuColor());

        this.add(jMenuItemModifColor);
        this.add(jMenuItemSupprColor);

        this.addPopupMenuListener(new ActionPopupMenuColor());
    }

    public void setJDParent(JDialogColorSettings JDParent) {
        this.JDParent = JDParent;
    }

    private class ActionPopupMenuColor implements ActionListener, PopupMenuListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int selectedIndex = JDParent.getJoueur().getListColors().indexOf(getInvoker().getBackground());
            if (e.getActionCommand().equals("Supprimer")) {
                JDParent.getJoueur().getListColors().remove(selectedIndex);
                JDParent.setJpListeColors();
                JDParent.setJbtAjouterEnabled();
                JDParent.setIsModifColor(true);
            }
             if (e.getActionCommand().equals("Modifier")) {
                Color colorChosen = JColorChooser.showDialog(null, "Modification de la couleur", getInvoker().getBackground());
                 if (Tools.isColorNotUse(JDParent.getJoueur(), colorChosen)) {
                     getInvoker().setBackground(colorChosen);
                     JDParent.getJoueur().getListColors().set(selectedIndex,colorChosen);
                     JDParent.setIsModifColor(true);
                 }
            }
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            if (JDParent.getJoueur().getListColors().size() > 6) {
                jMenuItemSupprColor.setEnabled(true);
            }
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

        public void popupMenuCanceled(PopupMenuEvent e) {
        }
    }
}
